package com.qiall.boot.fileservice.impl;

import com.qiall.boot.fileservice.StreamProcessInServer;
import com.qiall.boot.fileservice.data.eitity.FileInfo;
import com.qiall.boot.fileservice.data.mapper.FileInfoMapper;
import com.qiall.boot.fileservice.data.properties.MinioProperties;
import com.qiall.boot.fileservice.impl.fileuploadSuccessEvent.UploadSuccessEvent;
import com.qiall.common.exception.BusinessException;
import com.qiall.common.exception.ResultCode;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component("minioFileService")
public class MinioFileServiceImpl implements StreamProcessInServer {

    @Autowired
    private MinioClient minioClient;

    private FileInfoMapper fileInfoMapper;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private MinioProperties properties;

    @Override
    public String getDownLoadUrl(String fromId) {
        String objectName = getObjectNameFromId(fromId);
        String tmpUrl = null;
        try {
            tmpUrl = minioClient.getObjectUrl(properties.getBucketName(), objectName);
        } catch (Exception e) {
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "文件获取失败" + e.getMessage());
        }
        return tmpUrl;
    }

    @Override
    public void deleteFie(String fromId) {
        String objectName = getObjectNameFromId(fromId);
        try {
            minioClient.removeObject(properties.getBucketName(), objectName);
        } catch (Exception e) {
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "文件删除失败" + e.getMessage());
        }
    }

    @Override
    public void uploadFile(MultipartFile multipartFile, Integer fileId) {

        String fileName = multipartFile.getOriginalFilename();
        String objectName = buildObjectName(fileName);
        try {
            minioClient.putObject(properties.getBucketName(), objectName, multipartFile.getInputStream(), new PutObjectOptions(multipartFile.getInputStream().available(), -1));
        } catch (Exception e) {
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "文件上传失败" + e.getMessage());
        }
        String fromId = buildFromId(properties.getBucketName(), objectName);
        //发布上传成功的event
        UploadSuccessEvent uploadSuccessEvent = new UploadSuccessEvent(this, fileId, fromId);
        applicationContext.publishEvent(uploadSuccessEvent);
    }


    @Override
    public String uploadFileInner(MultipartFile multipartFile, Integer fileId) {
        String orangeName = multipartFile.getOriginalFilename();
        String objectName = buildObjectName(orangeName);
        FileInfo fileInfo = new FileInfo();
        fileInfo.setId(fileId);
        try {
            minioClient.putObject(properties.getBucketNameInner(), objectName, multipartFile.getInputStream(), new PutObjectOptions(multipartFile.getInputStream().available(), -1));
            String fromIdInner = buildFromId(properties.getBucketNameInner(), objectName);
            fileInfo.setFromId(fromIdInner);
            fileInfoMapper.update(fileInfo);
            return minioClient.getObjectUrl(properties.getBucketNameInner(), objectName);
        } catch (Exception e) {
            throw new BusinessException(ResultCode.SYSTEM_ERROR, "文件上传失败" + e.getMessage());
        }
    }

    private String buildObjectName(String fileName) {
        long time = System.currentTimeMillis();
        return time + fileName;
    }

    private String buildFromId(String bucketName, String objectName) {
        return bucketName + "_" + objectName;
    }

    private String getObjectNameFromId(String fromId) {
        String prefix = properties.getBucketName() + "_";
        String objectName = fromId.replace(prefix, "");
        return objectName;
    }

}
